package com.yanqu.road.server.manager.systemgroup;

import java.util.ArrayList;
import java.util.List;

public class CrossSystemWeekGroupMatchHelper {


    /**
     * 分组（都均摊）
     * @param numPerGroup  每一组个数
     * @param minNumPerGroup  最小独立成组个数
     * @param totalNum 总个数
     * @return
     */
    public static List<Integer> divideGroupNum(int numPerGroup, int minNumPerGroup, int totalNum) {
        List<Integer> groupNumList = new ArrayList<>();
        //可分为n组
        int groupNum = totalNum / numPerGroup;
        for (int i = 0; i < groupNum; i++) {
            groupNumList.add(numPerGroup);
        }
        int yuShu = totalNum % numPerGroup;
        //大于临界数，组数+1
        if(yuShu >= minNumPerGroup || groupNum == 0){  //兼容仅有一组且小于最小分组數量时
            groupNum++;
            groupNumList.add(yuShu);
        }
        //均摊
        int newNumPerGroup = totalNum / groupNum;
        yuShu = totalNum % groupNum;

        //每个分组均分加add
        for (int i = 0; i < groupNumList.size(); i++) {
            groupNumList.set(i, newNumPerGroup);
        }
        //余数，从最后分组往前分配
        for (int i = groupNumList.size() - 1; i >= 0 && yuShu > 0; i--) {
            groupNumList.set(i, groupNumList.get(i) + 1);
            yuShu--;
        }
        Cross3SystemWeekGroupMatchMgr.getLogger().info("numPerGroup {}, minNumPerGroup {}, totalNum {}, groupNumList:{}", numPerGroup, minNumPerGroup, totalNum, groupNumList);
        return groupNumList;
    }

    /**
     * 分组(最小组独立成组)
     * @param numPerGroup  每一组个数
     * @param minNumPerGroup  最小独立成组个数
     * @param totalNum 总个数
     * @return
     */
    public static List<Integer> divideGroupNum2(int numPerGroup, int minNumPerGroup, int totalNum) {
        List<Integer> groupNumList = new ArrayList<>();
        int groupNum = totalNum / numPerGroup;
        for (int i = 0; i < groupNum; i++) {
            groupNumList.add(numPerGroup);
        }
        int yuShu = totalNum % numPerGroup;
        if(yuShu >= minNumPerGroup || groupNum == 0){
            //余数独立成一组
            groupNumList.add(yuShu);
        }else {
            int add = yuShu / groupNumList.size();
            int num = yuShu % groupNumList.size();
            //每个分组均分加add
            for (int i = 0; i < groupNumList.size(); i++) {
                groupNumList.set(i, groupNumList.get(i) + add);
            }
            //余数，从最后分组往前分配
            for (int i = groupNumList.size() - 1; i >= 0 && num > 0; i--) {
                groupNumList.set(i, groupNumList.get(i) + 1);
                num--;
            }
        }
        Cross3SystemWeekGroupMatchMgr.getLogger().info("numPerGroup {}, minNumPerGroup {}, totalNum {}, groupNumList:{}", numPerGroup, minNumPerGroup, totalNum, groupNumList);
        return groupNumList;
    }
}
